from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 设置Chrome浏览器选项
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)

try:
    # 访问上传页面
    driver.get("file:///F:/dowload%E4%B8%8B%E8%BD%BD/TestProject/TestProject/upload.html")  # 替换为实际的上传页面URL

    # 确保页面加载完成
    WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.NAME, "uploadFile"))
    )

    # 找到文件选择按钮
    file_input = driver.find_element(By.NAME, "uploadFile")  # 根据实际页面的文件输入框ID进行调整

    # 上传文件
    file_path = r"F:\1118evening.zip" # 替换为实际的文件路径，确保路径是全英文的
    file_input.send_keys(file_path)

    # 验证文件是否被选中
    if file_input.get_attribute("value") == file_path:
        print("文件已成功选中")
    else:
        print("文件未成功选中")

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    # 截图保存
    driver.save_screenshot("upload_screenshot.png")

    # 关闭浏览器
    driver.quit()